package com.ice_col.camerademo;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

    private Button bt_take_photo;
    private Button bt_take_video;
    private ImageView iv;
    private VideoView videoView;

    private void init(){
        bt_take_photo=(Button) findViewById(R.id.bt_take_photo);
        iv=(ImageView) findViewById(R.id.iv);
        bt_take_video = (Button) findViewById(R.id.bt_take_video);
        videoView=(VideoView) findViewById(R.id.video);
        bt_take_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 0);
            }
        });
        bt_take_video.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                startActivityForResult(intent, 1);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode==0&& Activity.RESULT_OK==resultCode){
            Bitmap bitmap=(Bitmap) data.getExtras().get("data");
            iv.setImageBitmap(bitmap);
        }else if(requestCode==1&& Activity.RESULT_OK==resultCode){

            Uri uri=data.getData();
            Cursor cursor=getContentResolver().query(uri,null, null, null, null);
            if(cursor.getCount()!=0){
                cursor.moveToFirst();
                String videoPath=cursor.getString(cursor.getColumnIndex("_data"));
                videoView.setVideoURI(Uri.parse(videoPath));
                videoView.setMediaController(new MediaController(this));
                videoView.start();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
